package com.truongnha.clienttn.data.dao;

import com.truongnha.clienttn.data.entity.AccountEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AccountDAO {
	private final SQLiteDatabase db;

	public AccountDAO(SQLiteDatabase db) {
		this.db = db;
	}

	public void delete() {
		db.delete(AccountScheme.TABLE_NAME, null, null);
	}

	public long insert(String last_name, String first_name, String account,
			String userType, String _class, String school, String birthday,
			String userId, String positionId, String csrftoken) {
		ContentValues inititalValue = new ContentValues();
		inititalValue.put(AccountScheme.LAST_NAME, last_name);
		inititalValue.put(AccountScheme.FIRST_NAME, first_name);
		inititalValue.put(AccountScheme.USER_NAME, account);
		inititalValue.put(AccountScheme.USER_TYPE, userType);
		inititalValue.put(AccountScheme.CLASS, _class);
		inititalValue.put(AccountScheme.SCHOOL, school);
		inititalValue.put(AccountScheme.BIRTHDAY, birthday);
		inititalValue.put(AccountScheme.USER_ID, userId);
		inititalValue.put(AccountScheme.POSITION_ID, positionId);
		inititalValue.put(AccountScheme.CSRFTOKEN, csrftoken);
		return db.insert(AccountScheme.TABLE_NAME, null, inititalValue);
	}

	public AccountEntity getAccountByUser(String userName) {
		String sql = "SELECT * from Account where user_name = '" + userName
				+ "'";
		Cursor mcursor = db.rawQuery(sql, null);
		mcursor.moveToFirst();
		return new AccountEntity(mcursor.getInt(0), mcursor.getString(1),
				mcursor.getString(2), mcursor.getString(3),
				mcursor.getString(4), mcursor.getString(5),
				mcursor.getString(6), mcursor.getString(7),
				mcursor.getString(8), mcursor.getString(9),
				mcursor.getString(10));
	}

	public AccountEntity getCurrentAccount() {
		String sql = "SELECT * from Account ";
		Cursor mcursor = db.rawQuery(sql, null);
		mcursor.moveToFirst();
		return new AccountEntity(mcursor.getInt(0), mcursor.getString(1),
				mcursor.getString(2), mcursor.getString(3),
				mcursor.getString(4), mcursor.getString(5),
				mcursor.getString(6), mcursor.getString(7),
				mcursor.getString(8), mcursor.getString(9),
				mcursor.getString(10));
	}
}